﻿using Microsoft.TeamFoundation.Client;
using Microsoft.TeamFoundation.VersionControl.Client;
using Microsoft.TeamFoundation.VersionControl.Common;
using Microsoft.Win32;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace TeamExplorer.Merge
{
    public class VersionControlSettings
    {
        protected static VersionControlSettings s_instance = (VersionControlSettings)null;

        protected static readonly string VCRegistryRootDefault = "TeamFoundation\\SourceControl";
        protected static readonly string GeneralLastWorkspaceString = "LastWorkspace";

        private RegistryKey GetFeatureServerKey(string featurePath, string serverGuid)
        {
            using (RegistryKey registryKey = UIHost.UserRegistryRoot)
            {
                if (registryKey != null)
                {
                    string str = VCRegistryRootDefault + "\\" + featurePath + "\\" + serverGuid;
                    return registryKey.OpenSubKey(str, true);
                }
            }
            return (RegistryKey)null;
        }

        public Workspace GetLastWorkspaceForServer(VersionControlServer vcServer, string viewName)
        {
            Workspace workspace = (Workspace)null;
            try
            {
                string workspaceSpecForServer = this.GetLastWorkspaceSpecForServer(vcServer, viewName);
                if (!string.IsNullOrEmpty(workspaceSpecForServer))
                {
                    
                        string workspaceName;
                        string workspaceOwner;
                        WorkspaceSpec.Parse(workspaceSpecForServer, vcServer.AuthorizedUser, out workspaceName, out workspaceOwner);
                        WorkspaceInfo localWorkspaceInfo = Workstation.Current.GetLocalWorkspaceInfo(vcServer, workspaceName, workspaceOwner);
                        if (localWorkspaceInfo != (WorkspaceInfo)null)
                            workspace = localWorkspaceInfo.GetWorkspace(vcServer.TeamProjectCollection);
                }
            }
            catch (Exception)
            {
            }
            return workspace;
        }

        public static VersionControlSettings Instance
        {
            get
            {
                if (VersionControlSettings.s_instance == null)
                    VersionControlSettings.s_instance = new VersionControlSettings();
                return VersionControlSettings.s_instance;
            }
        }

        private string GetLastWorkspaceSpecForServer(VersionControlServer vcServer, string viewName)
        {
            string str1 = (string)null;
            try
            {
                if (vcServer != null)
                {
                    if (!string.IsNullOrEmpty(viewName))
                    {
                        using (RegistryKey featureServerKey = this.GetFeatureServerKey(viewName, vcServer.ServerGuid.ToString()))
                        {
                            if (featureServerKey != null)
                            {
                                string str2 = featureServerKey.GetValue(VersionControlSettings.GeneralLastWorkspaceString) as string;
                                if (!string.IsNullOrEmpty(str2))
                                    str1 = str2;
                            }
                        }
                    }
                }
            }
            catch (Exception)
            {
            }
            return str1;
        }

        internal static class LastWorkspaceSource
        {
            public static readonly string Explorer = "Explorer";
            public static readonly string PendingCheckins = "PendingCheckins";

            static LastWorkspaceSource()
            {
            }
        }
    }
}
